<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>await函数</title>
</head>

<body>
    <script>
        async function main() {
            let promise = new Promise((resolve, reject) => {
                resolve('OK')
                // reject('Error')
            })
            //  1. 右侧为promise对象的情况
            let result = await promise
            console.log(result)
            // 2. 右侧是其他类型的数据,很少使用，一般都是Promise对象
            let result2 = await 20
            console.log(result2)
            // 3. 如果Promise是失败的状态,这时可以使用try-catch捕获异常
            let promise2 = new Promise((resolve, reject) => {
                reject('Error')
            })
            try {
                // 会在这行抛错，下面的console.log就不会执行
                let result3 = await promise2
                console.log('result3:', result3)
            } catch (e) {
                console.log(e)
            }
        }
        main()
    </script>
</body>

</html>